package com.hmsg.health.service.admin;

import com.hmsg.health.bean.Dto.UmengExtra;
import com.hmsg.health.bean.Dto.WithdrawDto;
import com.hmsg.health.bean.domain.UserAccountDo;
import com.hmsg.health.bean.domain.UserAccountRecordDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.domain.WithdrawDo;
import com.hmsg.health.dao.UserAccountDao;
import com.hmsg.health.dao.UserAccountRecordDao;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.dao.WithdrawDao;
import com.hmsg.health.service.UserService;
import com.hmsg.health.utils.umeng.push.Demo;

import lombok.extern.slf4j.Slf4j;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminWithDrawService {
    @Autowired
    private WithdrawDao withdrawDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserAccountDao userAccountDao;
    @Autowired
    private UserService userService;

    @Autowired
    private UserAccountRecordDao userAccountRecordDao;

    public List<WithdrawDto> findAll(Integer state,String phone,String time,
                                     Integer firstResult,Integer maxResult) {
        List<WithdrawDto> all = withdrawDao.findAll(state,phone,time,
                                                    firstResult,maxResult);
        if(all != null && all.size()>0){
            for(WithdrawDto w:all){
                UserDo select = userDao.select(w.getUserId());
                w.setTxUserName(select.getUserName());
                w.setTxPhone(select.getMobile());
            }
        }
        return all;
    }
    @Transactional
    public void updateAll(WithdrawDo withdrawDo) throws Exception {
        withdrawDao.update(withdrawDo);
       
        //退回金豆
        WithdrawDo one = withdrawDao.findOne(withdrawDo.getId());
        UserDo user = userDao.select(one.getUserId());
        UserAccountDo userAccount = userAccountDao.findUserAccount(one.getUserId());
        if(userAccount != null){
            UserAccountDo userAccountDo=new UserAccountDo();
            userAccountDo.setGlod(one.getGload());
            userAccountDo.setUserId(one.getUserId());
            userAccountDao.update(userAccountDo);
        }else{
            UserAccountDo userAccountDo=new UserAccountDo();
            userAccountDo.setGlod(one.getGload());
            userAccountDo.setUserId(one.getUserId());
            userAccountDo.setCanWithdraw(0);
            userAccountDo.setScore(0);
            userAccountDo.setUserId(one.getId());
            int res1 = userService.saveUserAccount(userAccountDo);
        }

        //支付回退
        UserAccountRecordDo userAccountRecordDo1=new UserAccountRecordDo();
        userAccountRecordDo1.setAmount(one.getGload());
        userAccountRecordDo1.setUserId(one.getUserId());
        userAccountRecordDo1.setGlodType(1);
        userAccountRecordDo1.setType(9);
        userAccountRecordDo1.setCreateTime(new Date());

        userAccountRecordDo1.setTitle("打款失败返回金豆");
        userAccountRecordDao.save(userAccountRecordDo1);
        
        
		if(user.getDeviceToken()!=null&&user.getDeviceToken().length()==64) {
			Demo demo = new Demo();
			
			List<UmengExtra> list = new ArrayList<UmengExtra>();
			UmengExtra ue = new UmengExtra();
			ue.setKey("type");
			ue.setValue(8);
			list.add(ue);
			UmengExtra ue1 = new UmengExtra();
			ue1.setKey("resourceId");
			ue1.setValue(one.getId());
			list.add(ue1);
			String text = "非常抱歉，您的提现申请未能通过，请填写正确的支付宝账户重新提交。";
			
			JSONObject alert = new JSONObject();
			alert.put("title", "提现失败");
			//alert.put("subtitle", "审核拒绝通知");
			alert.put("body", text);
			demo.sendIOSUnicast(user.getDeviceToken(), alert,list);
			
		}
		
		if(user.getDeviceToken()!=null&&user.getDeviceToken().length()==44) {
			Demo demo = new Demo();
			List<UmengExtra> list = new ArrayList<UmengExtra>();
			UmengExtra ue = new UmengExtra();
			ue.setKey("type");
			ue.setValue(8);
			list.add(ue);
			UmengExtra ue1 = new UmengExtra();
			ue1.setKey("resourceId");
			ue1.setValue(one.getId());
			list.add(ue1);
			String text = "非常抱歉，您的提现申请未能通过，请填写正确的支付宝账户重新提交。";
			
			
			demo.sendAndroidUnicast(user.getDeviceToken(), "提现失败","提现失败",text, list);
			
		}

    }
}
